package com.example.restful.controller;

import com.example.restful.model.Student;
import com.example.restful.mapper.StudentRepository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/students")
@CrossOrigin
public class StudentController {

    private final StudentRepository studentRepository;

    public StudentController(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    @GetMapping
    public List<Student> getAll() {
        return studentRepository.findAll();
    }

    @GetMapping("/{id}")
    public Student getById(@PathVariable int id) {
        return studentRepository.findById(id);
    }

    @PostMapping
    public int add(@RequestBody Student student) {
        return studentRepository.insert(student);
    }

    @PutMapping
    public int update(@RequestBody Student student) {
        return studentRepository.update(student);
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable int id) {
        return studentRepository.delete(id);
    }
}
